package com.goods.admin.controller;

import com.goods.admin.service.AdminService;
import com.goods.entity.Admin;
import com.goods.entity.User;
import com.goods.utools.mail.Mail;
import com.goods.utools.mail.MailUtils;
import com.goods.utools.verifycode.Verify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

@Controller
@RequestMapping("/Admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    //    退出登录
    @RequestMapping("/adminLoginOut")
    public String loginOut(HttpSession session) {
        session.invalidate();
        return "adminjsps/login";
    }


    //    用户登录验证
    @RequestMapping("/AdminLogin")
    public String userLogin(Admin admin, Model model, HttpSession session) {
        Admin  admin1 = adminService.adminLogin(admin);
        if (admin1 == null) {
            model.addAttribute("loginMsg", "用户名或密码错误");
            return "adminjsps/login";
        } else {
            session.setAttribute("admin", admin1);
        }
        return "adminjsps/admin/main";
    }


    //校验验证码
    @RequestMapping("/VerifyVeifycode")
    @ResponseBody
    public Boolean verifyVeifycode(HttpSession session, String verifycode) {
        //1 从session中  拿到 正确的验证码的值
        String vCode = (String) session.getAttribute("vCode");
        //2 比较  不区分大小写
        if (vCode.equalsIgnoreCase(verifycode)) {
            return true;
        }
        return false;
    }


    //    验证码获取
    @RequestMapping("/CreatViriyCode")
    public void getViriy(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Verify.getVirify(request, response);
    }
}
